﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace ShopMobile_App
{
    public partial class frmItems : Form
    {
        public frmItems()
        {
            InitializeComponent();
        }

        private void frmItems_Load(object sender, EventArgs e)
        {
            ItemBLL it = new ItemBLL();
            dataGridViewItems.DataSource = it.getItemAllCat();
            CategoryBLL cat = new CategoryBLL();
            foreach (var x in cat.getCategories2()) 
            {
                comboBoxCategory.Items.Add(x);
            }
        }
       
        private void dataGridViewItems_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ItemBLL it = new ItemBLL();
            DataGridViewRow curRow = dataGridViewItems.CurrentRow;
            txtID.Text = curRow.Cells[0].Value.ToString();
            txtName.Text = curRow.Cells[1].Value.ToString();
            comboBoxCategory.Text = curRow.Cells[2].Value.ToString();
            txtPrice.Text = curRow.Cells[3].Value.ToString();
            txtID_Quantity.Text = curRow.Cells[11].Value.ToString();
            txtDisplay.Text = curRow.Cells[5].Value.ToString();
            txtCamera.Text = curRow.Cells[6].Value.ToString();
            txtConfig.Text = curRow.Cells[7].Value.ToString();
            txtConnect.Text = curRow.Cells[8].Value.ToString();
            txtData.Text = curRow.Cells[9].Value.ToString();
            txtGeneral.Text = curRow.Cells[10].Value.ToString();
            txtSaleOff.Text = curRow.Cells[12].Value.ToString();
            PtBx_Hinh.Image = ByteArrayToImage(it.getImage(txtID.Text));  //"C:\\Users\\ThienDuc\\Desktop\\Nhom03\\ShopMobile_App\\images\\items\\" + curRow.Cells[4].Value.ToString();
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ItemBLL it = new ItemBLL();
            dataGridViewItems.DataSource = it.getSearch(txtSearch.Text);
        }

        private void btnRefesh_Click(object sender, EventArgs e)
        {
            frmItems_Load(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ItemBLL it = new ItemBLL();
            it.name = txtName.Text;
            it.name_cat = comboBoxCategory.Text;
            it.price = int.Parse(txtPrice.Text);
            it.image = "8.jpg";
            it.image2 = ConvertImageToByte(PtBx_Hinh.Image, GetMimeType(PtBx_Hinh.Image));         
            it.display = txtDisplay.Text;
            it.quantity_id = int.Parse(txtID_Quantity.Text);
            it.camera = txtCamera.Text;
            it.configuration = txtConfig.Text;
            it.connectivity = txtConnect.Text;
            it.datatranfers = txtData.Text;
            it.generalfeatures = txtGeneral.Text;
            it.sale_off = int.Parse(txtSaleOff.Text);
            it.stt = comboBoxStatus.SelectedIndex;
            if (it.addItem(it))
            {
                MessageBox.Show("Thêm thành công!");
                frmItems_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Thêm thất bại!");
            }

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog dl = new OpenFileDialog();
            dl.Filter = "Image Files (*.jpg,*.png,*.gif,*.jpeg)|*.jpg;*.png;*.gif;*.jpeg";

            if (dl.ShowDialog() == DialogResult.OK)
            {
                txtImage.Text = dl.FileName;
                Image img = System.Drawing.Image.FromFile(txtImage.Text);
                PtBx_Hinh.Image = img;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            ItemBLL it = new ItemBLL();
            it.id = int.Parse(txtID.Text);
            it.name = txtName.Text;
            it.name_cat = comboBoxCategory.Text;
            it.price = int.Parse(txtPrice.Text);
            it.image = "8.jpg";
            it.image2 = ConvertImageToByte(PtBx_Hinh.Image, GetMimeType(PtBx_Hinh.Image));
            it.display = txtDisplay.Text;
            it.quantity_id = int.Parse(txtID_Quantity.Text);
            it.camera = txtCamera.Text;
            it.configuration = txtConfig.Text;
            it.connectivity = txtConnect.Text;
            it.datatranfers = txtData.Text;
            it.generalfeatures = txtGeneral.Text;
            it.sale_off = int.Parse(txtSaleOff.Text);
            it.stt = comboBoxStatus.SelectedIndex;
            if (it.editItem(it))
            {
                MessageBox.Show("Sửa thành công!");
                frmItems_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Sửa thất bại!");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ItemBLL it = new ItemBLL();
            if (it.delItem(int.Parse(txtID.Text)))
            {
                MessageBox.Show("Xóa thành công!");
                frmItems_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Xóa thất bại!");
            }
        }


        ////////////////
        #region Xu ly Image
        public static string GetMimeType(Image i)
        {
            foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())
            {
                if (codec.FormatID == i.RawFormat.Guid)
                    return codec.MimeType;
            }

            return "image/unknown";
        }

        public static byte[] ConvertImageToByte(Image i, string imageType)
        {

            MemoryStream ms = new MemoryStream();
            switch (imageType)
            {
                case "image/jpg":
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "image/jpeg":
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "image/png":
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case "image/gif":
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                default:
                    break;
            }
            return ms.ToArray();
        }

        public static Image ByteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
        #endregion
        #region Nhập liệu
        //chặn rỗng
        //private void txtQuantity_Validating(object sender, CancelEventArgs e)
        //{
        //    string stt="";
        //    if (txtQuantity.Text.Length == 0)
        //    {
        //        stt = "Lỗi";
        //    }
        //    else
        //    {
        //        e.Cancel = false;
        //    }
        //    errorProvider1.SetError(txtQuantity, stt);
        //}
        //chặn số
        //private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    char ch = e.KeyChar;
        //    if (!Char.IsDigit(ch) && ch!= 8)
        //        e.Handled = true;
        //}
        #endregion
    }
}
